﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Utils;

namespace LevenBlog.Core.Data.Sqlite
{
    public sealed class SqliteComment : IComment
    {
        public int Count(IDbProvider provider, CommentState state)
        {
            string sql = "select count([id]) from [lb_comment]";
            sql += " where ";
            sql += CreateWhereString(state);
            return Convert.ToInt32(SqliteHelper.Instance.ExecuteScalar(provider, sql));
        }

        public int Count(IDbProvider provider, int blogID, CommentState state)
        {
            string sql = string.Format("select count([id]) from [lb_comment] where [blogid]={0}", blogID);
            sql += CreateWhereString(state, string.Empty);
            return Convert.ToInt32(SqliteHelper.Instance.ExecuteScalar(provider, sql));
        }

        public void Insert(IDbProvider provider, lb_comment comment)
        {
            IDataParameter[] paras = null;
            string sql = SqliteHelper.Instance.CreateInsertSql(comment, out paras);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Update(IDbProvider provider, lb_comment comment)
        {
            IDataParameter[] paras = null;
            string sql = SqliteHelper.Instance.CreateUpdateSql(comment, out paras);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql, paras);
        }

        public void Delete(IDbProvider provider, int id)
        {
            string sql = string.Format("delete from [lb_comment] where [id]={0}", id);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public void DeleteFromBlogID(IDbProvider provider, int blogID)
        {
            string sql = string.Format("delete from [lb_comment] where [blogid]={0}", blogID);
            SqliteHelper.Instance.ExecuteNonQuery(provider, sql);
        }

        public lb_comment Get(IDbProvider provider, int id)
        {
            string sql = string.Format("select * from [lb_comment] where [id]={0} order by [id] desc", id);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToEntity<lb_comment>(dr);
            }
        }

        public IList<lb_comment> Get(IDbProvider provider, int pageSize, int currentPage, CommentState state)
        {
            if (currentPage < 1)
                currentPage = 1;
            //string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_comment]", string.Empty, "[id]", " order by [id] desc");
            string sql = string.Format("select * from [lb_comment] where {0} order by [id] desc limit {1},{2}", CreateWhereString(state), (currentPage - 1) * pageSize, pageSize);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToList<lb_comment>(dr);
            }
        }

        public IList<lb_comment> GetFromBlogID(IDbProvider provider, int blogID, int pageSize, int currentPage, CommentState state)
        {
            if (currentPage < 1)
                currentPage = 1;
            //string sql = PageHelper.BuildPageSql(pageSize, currentPage, "*", "[lb_comment]", string.Format("[blogid]={0}", blogID), "[id]", " order by [id] asc");
            string sql = string.Format("select * from [lb_comment] where [blogid]={0} {1} order by [id] asc limit {2},{3}", blogID, CreateWhereString(state, string.Empty), (currentPage - 1) * pageSize, pageSize);
            using (IDataReader dr = SqliteHelper.Instance.ExecuteDataReader(provider, sql))
            {
                return SqliteHelper.Instance.ConvertToList<lb_comment>(dr);
            }
        }

        private string CreateWhereString(CommentState state, string innerWhere)
        {
            StringBuilder sb = new StringBuilder(innerWhere);
            if ((CommentState.Show & state) == CommentState.Show)
            {
                sb.Append(" and [isshow]=1");
            }
            if ((CommentState.NotShow & state) == CommentState.NotShow)
            {
                sb.Append(" and [isshow]=0");
            }
            if ((CommentState.NotMailNew & state) == CommentState.NotMailNew)
            {
                sb.Append(" and [ismailnew]=0");
            }
            if ((CommentState.MailNew & state) == CommentState.MailNew)
            {
                sb.Append(" and [ismailnew]=1");
            }
            return sb.ToString();
        }

        private string CreateWhereString(CommentState state)
        {
            return CreateWhereString(state, "1=1");
        }
    }
}
